package org.example.ecommerce_system.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.ecommerce_system.entity.Order;
import org.example.ecommerce_system.entity.Product;
import org.example.ecommerce_system.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//6
@RestController
@CrossOrigin(origins = "*")
public class OrderController {

    @Autowired
    OrderMapper orderMapper;

    @RequestMapping("/getAllOrder")
    public PageInfo<Order> getAllOrder(@RequestParam(defaultValue = "1") int currentPage,
                                   @RequestParam(defaultValue = "5") int pageSize){
        try {
            PageHelper.startPage(currentPage, pageSize);
            List<Order> orders = orderMapper.getAllOrder();
            return new PageInfo<>(orders);
        } finally {
            PageHelper.clearPage(); // 确保清除分页设置
        }
    }

    @RequestMapping("/searchOrder")
    public PageInfo<Order> searchName(@RequestParam String userName, @RequestParam(defaultValue = "1") int currentPage,
                                  @RequestParam(defaultValue = "5") int pageSize){
        try {
            PageHelper.startPage(currentPage, pageSize);
            List<Order> orders = orderMapper.searchName(userName);
            return new PageInfo<>(orders);
        } finally {
            PageHelper.clearPage();
        }
    }

    @RequestMapping("/searchCombin")
    public PageInfo<Order> searchCombin(@RequestParam String userName, @RequestParam String proName,
                                       @RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "5") int pageSize){
        try {
            PageHelper.startPage(currentPage, pageSize);
            List<Order> orders = orderMapper.searchCombin(userName,proName);
            return new PageInfo<>(orders);
        } finally {
            PageHelper.clearPage();
        }
    }

    @RequestMapping("/deleteOrder")
    public int deleteOrder(int id){
        return orderMapper.deleteOrder(id);
    }

    @RequestMapping("/insertOrder")
    public int insertOrder(@RequestBody Order order){
        return orderMapper.insertOrder(order);
    }

    @RequestMapping("/updateOrder")
    public int updateOrder(@RequestBody Order order){

        return orderMapper.updateOrder(order);
    }
}
